import { Main } from "./Main";



//位置类型
export enum postype{
    /** 左上 */
    up_left = 0,
    up_right = 1,
    right = 2,
    down_right = 3,
    down_left = 4,
    left = 5
}





const {ccclass, property} = cc._decorator;

@ccclass
export class MoveHexa extends cc.Component {

    
    private _locationPos:number = 0;
    private _level:number = 0;

    /** 起始变化角度 */
    private _changeangle:number = 0;

    /** 角度变化范围 */
    private _range = 0;

    /** 节点当前位置 */
    private _currentPos:cc.Vec2 = null;

    /** 当前位置类型 */
    private _postype:postype = 0;

    /** 是否执行交换 */
    private _isperformexchange:boolean = false;

    hexaWidth: number;

    /** 横坐标符号 */
    private Abscissa_symbol = 0;

    private Ordinate_symbol = 0;

    private callback:Function;

    set startChange (boll:boolean){

        this._isperformexchange = boll;
    }

    // onLoad () {}
    set Level (pos:number){

        this._level = pos;
    }

    get Level (){
        return this._level;
    }

    set locationPos (pos:number){

        this._locationPos = pos;
    }

    get locationPos (){
        return this._locationPos;
    }

    onLoad (){
        
        this.hexaWidth = Main.Map.MapfragmentsWidth/2;
    }

    public initview (){

        this._currentPos = this.node.position;
        this.judgeType();
    }

    /** 判断当前位置类型 */
    judgeType (){

        if(this._currentPos.x < 0 && this._currentPos.y > 0){

            this._postype = postype.up_left;
            this._changeangle = -30;

        }else if(this._currentPos.x > 0 && this._currentPos.y > 0){

            this._postype = postype.up_right;
            this._changeangle = 30;

        }else if(this._currentPos.x > 0 && this._currentPos.y == 0){

            this._postype  = postype.right;
            this._changeangle = 90;

        }else if(this._currentPos.x > 0 && this._currentPos.y < 0){

            this._postype  = postype.down_right;
            this._changeangle = 150;

        }else if(this._currentPos.x < 0 && this._currentPos.y < 0){

            this._postype  = postype.down_left;
            this._changeangle = 210;

        }else if(this._currentPos.x < 0 && this._currentPos.y == 0){

            this._postype  = postype.left;
            this._changeangle = -90;
        }
    }

    start () {

    }


    public startChangepos (complete:Function){

        this._isperformexchange = true;
        this.callback = complete;
    }

    update (dt) {

        //开始交换位置
        if(this._isperformexchange){

            this._range += dt*100;
            let range = this._changeangle + this._range;
            if(this._range >= 60){             
                this._range = 60;
                range = this._changeangle + this._range;

                let posx = this.hexaWidth*Math.sin(range*Math.PI /180);
                let posy = this.hexaWidth*Math.cos(range*Math.PI /180);
                if(range == 90 || range == 270){
                    posy = 0;
                }
                this.node.setPosition(posx,posy); 
                this._isperformexchange = false;
                this._range = 0;
                this._currentPos = cc.v2(posx,posy);
                this.judgeType();
                if(this.callback)this.callback();
                return;
            }
            
            let posx = this.hexaWidth*Math.sin(range*Math.PI /180);
            let posy = this.hexaWidth*Math.cos(range*Math.PI /180);

            this.node.setPosition(posx,posy);  
        }
    }
}
